package com.intelligent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.pojo.Quotation;
import com.intelligent.service.QuotationService;
import com.intelligent.utils.Result;
import com.intelligent.vo.QuotationPageReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 报价单管理控制器
 * 负责前端接口，业务逻辑由Service层处理
 */
@RestController
@RequestMapping("/quotation")
public class QuotationController {

    @Autowired
    private QuotationService quotationService;

    /**
     * 分页查询报价单
     */
    @PostMapping("/page")
    public Result<Page<Quotation>> pageList(@RequestBody QuotationPageReq quotationPageReq) {
        try {
            Page<Quotation> result = quotationService.pageWithRelations(quotationPageReq);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 新建报价单
     */
    @PostMapping
    public Result<Quotation> create(@RequestBody Quotation quotation) {
        try {
            Quotation result = quotationService.createQuotation(quotation);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("创建失败：" + e.getMessage());
        }
    }

    /**
     * 查看报价单详情
     */
    @GetMapping("/{id}")
    public Result<Quotation> detail(@PathVariable Long id) {
        try {
            Quotation quotation = quotationService.getQuotationDetail(id);
            if (quotation == null) {
                return Result.error("未找到数据");
            }
            return Result.success(quotation);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }
}
